package com.ag.zrt.service.impl;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ag.bean.TbInforType;
import com.ag.bean.TbInformation;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;
import com.ag.zrt.dao.TbInforTypeDAO;
import com.ag.zrt.dao.TbInformationDAO;
import com.ag.zrt.service.InformationService;

@Service("InformationService")
public class InformationServiceImpl implements InformationService {
	@Resource(name = "ZRTTbInformationDAO")
	private TbInformationDAO informationDAO;
	@Resource(name = "ZRTTbInforTypeDAO")
	private TbInforTypeDAO inforTypeDAO;
	@Resource(name = "iDServiceImpl")
	private IIDService idService;

	private int totalPage; // 总页数
	private int all; // 符合条件的简报数量

	public int getAll() {
		return all;
	}

	public int getNewInfo() { // 未读简报条数
		String hql = "select count(*) from TbInformation if where if.b = "
				+ IDUtil.PRESERVE;
		return informationDAO.countAll(hql);
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setInformationDAO(TbInformationDAO informationDAO) {
		this.informationDAO = informationDAO;
	}

	public void setIdService(IIDService idService) {
		this.idService = idService;
	}

	@Override
	public TbInformation findById(String id) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb
				.append("from TbInformation info join fetch info.tbInforType where info.IId = ?");
		TbInformation tif = informationDAO.findById(id, sb.toString());
		if (tif.getB().equals("0")) {
			tif.setB("1");// 设置成已读
			informationDAO.attachDirty(tif);
			tif.setB("0");// 设置回原未读状态
		}

		return tif;
	}

	@Override
	public void save(TbInformation info) throws Exception {
		// TODO Auto-generated method stub
		String id = idService.findTablePK(IDUtil.TB_INFORMATION);
		info.setIId(id);

		info.setIDate(new Date());
		//info.setIState(1L);
		info.setIState(1);
		info.setA(IDUtil.PRESERVE);
		info.setB("0");// 0 表示未读
		// System.out.println(info.getIDate()+"-----------------------------------------------------------");
		informationDAO.save(info);

	}

	@Override
	public void update(TbInformation info) {
		// TODO Auto-generated method stub@Override
		TbInformation tInfo = informationDAO.findById(info.getIId());
		tInfo.setIInfo(info.getIInfo());
		tInfo.setIDetial(info.getIDetial());
		TbInforType it = inforTypeDAO.findById(info.getTbInforType().getItId());
		tInfo.setTbInforType(it);
		informationDAO.attachDirty(tInfo);
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		TbInformation info = informationDAO.findById(id);
		info.setA(IDUtil.DELETE);
		informationDAO.attachDirty(info);

	}

	@Override
	public void ratify(TbInformation info) {
		// TODO Auto-generated method stub
		TbInformation tif = informationDAO.findById(info.getIId());
		//long state = info.getIState();
		int state = info.getIState();
		tif.setIState(state);
		informationDAO.attachDirty(tif);

	}

	@Override
	public int countAll(String countType) { // 计算总记录数
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		String columKey = "info.a =" + IDUtil.PRESERVE;// 默认计算所有没有被删的记录数

		if (countType.equals("history")) { // 列出历史简报（被删的）
			columKey = "info.a =" + IDUtil.DELETE;
		}

		else if (countType.equals("newInformation")) { // 列出未读简报
			columKey = "info.b = 0";
		}
		sb.append("select count(*) from TbInformation info where " + columKey);
		int allNum = informationDAO.countAll(sb.toString());// 记录总数

		return allNum;
	}

	@Override
	public int getNewPage(int currentPage, int nextOrPrev) {// 根据翻页标志位产生新页码
		if (nextOrPrev == 1) {
			currentPage++;
		} else {
			currentPage--;
		}

		return currentPage;
	}

	@Override
	public int getTotalPages(String type) {
		// TODO Auto-generated method stub
		int totalPages = 1;// 总页数
		int allNum = countAll(type);

		if (allNum > 0) { // 计算页数
			if (allNum % IDUtil.INFORITEMS == 0) {
				totalPages = allNum / IDUtil.INFORITEMS;
			} else {
				totalPages = allNum / IDUtil.INFORITEMS + 1;
			}
		}
		return totalPages;
	}

	@Override
	public List<TbInformation> findOrSearchByPage(String searchTitle,
			String searchDate, String searchType, int searchRatify,
			int searchRead, String searchArea, int currentPage, int nextOrPrev,
			int sortWay) {
		// TODO Auto-generated method stub
		String title = "";
		if (!searchTitle.equals("")) {
			title = "tif.IInfo like '%" + searchTitle + "%'" + " and ";
		}

		String date = "";
		if (!searchDate.equals("")) { // 日期

			date = " tif.IDate >= to_date('"
					+ searchDate
					+ " 00:00:00','yyyy-MM-dd hh24:mi:ss') and tif.IDate <= to_date('"
					+ searchDate + " 23:59:59','yyyy-MM-dd hh24:mi:ss') and "; // 因为数据库存的具体到秒，而页面传进来的只具体到日
		}

		String type = "";
		if (!searchType.equals("")) {
			type = "tif.tbInforType.itId = " + searchType + " and ";
		}

		String ratify = "";
		if (searchRatify != 0) { // 审核状态
			ratify = "tif.IState = " + searchRatify + " and ";
		}

		String read = "";
		if (searchRead != 2) { // 阅读状态
			read = "tif.b = " + searchRead + " and ";
		}

		String area = "";
		if (searchArea.equals("shistory")) { // 搜索范围（全部或历史）
			area = "tif.a = " + IDUtil.DELETE;
		}

		if (searchArea.equals("all_area")) {
			area = "tif.a = " + IDUtil.PRESERVE;
		}

		String sort = ""; // 排序方式
		if (sortWay == 100) {
			sort = "tif.IDate ";
		}
		if (sortWay == 101) {
			sort = "tif.tbInforType.itId ";
		}
		if (sortWay == 102) {
			sort = "tif.IState ";
		}

		StringBuilder hql = new StringBuilder();
		hql.append("from TbInformation tif join fetch tif.tbInforType where "
				+ title + date + type + ratify + read + area
				+ " order by tif.b asc, " + sort + " desc");

		System.out.println(hql.toString()
				+ "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

		if (nextOrPrev == 0) {
			currentPage -= 2;
		}

		all = informationDAO // 得到总数
				.countAll("select count(*) from TbInformation tif where "
						+ title + date + type + ratify + read + area);

		if (all > 0) { // 计算页数
			if (all % IDUtil.INFORITEMS == 0) {
				totalPage = all / IDUtil.INFORITEMS;
			} else {
				totalPage = all / IDUtil.INFORITEMS + 1;
			}
		}

		System.out.println(all + "************************" + totalPage
				+ "***********************************");

		return informationDAO.find(hql.toString(), currentPage
				* IDUtil.INFORITEMS);
	}

}
